package com.yingside.jdbc.crm;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTable;
import com.lovo.netCRM.component.LovoTxt;
import com.yingside.jdbc.Teacher;
import com.yingside.jdbc.gui.TeacherDao;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.util.List;

public class EmployeeFrame extends JFrame {

    private LovoTable table;
    private LovoTxt txtName;
    private LovoButton btnDel,btnAdd,btnUpdate,btnQuery;
    private EmployeeDao employeeDao = new EmployeeDao();
    private String empName = "";

    public EmployeeFrame(){
        this.setTitle("员工管理界面");
        this.setBounds(200,100,500,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);

        initTable();
        initData();
        initButton();
    }
    public void initButton(){
        txtName = new LovoTxt("按姓名查询:",30,30,this);
        btnQuery = new LovoButton("查询",250,30,this);
        btnDel = new LovoButton("删除",30,320,this);
        btnAdd = new LovoButton("新增",130,320,this);
        btnUpdate = new LovoButton("更新",230,320,this);

        btnQuery.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                empName = txtName.getText();

                initData();
            }
        });

        btnUpdate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int key = table.getKey();
                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选择一行再进行更新操作");
                    return;
                }

                EmployeeUpdateFrame employeeUpdateFrame = new EmployeeUpdateFrame(key,EmployeeFrame.this);
                employeeUpdateFrame.setVisible(true);

            }
        });

        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                EmployeeAddFrame addFrame = new EmployeeAddFrame(EmployeeFrame.this);
                addFrame.setVisible(true);
            }
        });

        btnDel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //获取主键id
                int key = table.getKey();

                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选中一行再进行删除");
                    return;
                }

                int m = JOptionPane.showConfirmDialog(null,"你确定要删除吗?","删除确定",JOptionPane.OK_CANCEL_OPTION);

                if(m == 0){
                    boolean flag = employeeDao.delete(key);

                    if(flag){
                        JOptionPane.showMessageDialog(null,"删除成功");
                        initData();
                    }
                    else{
                        JOptionPane.showMessageDialog(null,"删除失败");
                    }
                }

            }
        });
    }

    public void initTable(){
        table = new LovoTable(this,
                new String[]{"编号","员工姓名","员工电话","员工部门"},
                new String[]{"id","empName","empTel","dept.deptName"},
                "id");
        table.setSizeAndLocation(30,80,400,200);
    }

    public void initData(){
        List<Employee> list = employeeDao.queryLikeEmpName(empName);
        table.updateLovoTable(list);
    }

    public static void main(String[] args) {
        EmployeeFrame teacherMainFrame = new EmployeeFrame();
        teacherMainFrame.setVisible(true);
    }
}
